package com.yfp.device.controller.database.redis;


import com.yfp.device.domain.DevicePreOrder;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class DevicePreOrderRedis {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取预支付订单
     * @param preOrderId
     * @return
     */
    public Optional<DevicePreOrder> getDevicePreOrder(String preOrderId) {
        //如果不存在主key那么直接不匹配
        String saveKey = "Order__devicePreOrder" + preOrderId;
        Boolean keyStatus =  redisTemplate.hasKey(saveKey);
        if(keyStatus == null || !keyStatus) return Optional.empty();
        DevicePreOrder devicePreOrder = (DevicePreOrder) redisTemplate.opsForValue().get(saveKey);
        return Optional.ofNullable(devicePreOrder);
    }

}